(** The sample module of generated parser with Castling.
This name [Generated] depends on your filename. *)

(** This module has abstract syntax tree types and pretty printers.
This name [AST] depends on argument [?ast_module] of calling {!Castling.ParserGenerator.generate}. *)
module AST : sig

	(** Generated types holding abstract syntax tree.
	Names of these types depend on argument of calling {!Castling.ParserGenerator.one_of} or syntax.
	This sample was generated from [one_of "variant" ["x", element 'x' >> element '1'; "y", position_with (element 'y'); error "z", element 'z']]. *)
	type 'a variant =
		| X of (Token.t option * Token.t option)
		| Y of (('a * 'a) * Token.t option)

	val pp_option :
		(Format.formatter -> 'a -> unit) -> paren:bool ->
		Format.formatter -> 'a option -> unit
	(** Pretty printer for ['a option].
	This function be generated only if {!Castling.ParserGenerator.pretty_printer} was [true]. *)

	val pp_position_pair :
		(Format.formatter -> 'a -> unit) -> Format.formatter -> 'a * 'a -> unit
	(** Pretty printer for pair of positions.
	This function be generated only if {!Castling.ParserGenerator.ranged_position} and {!Castling.ParserGenerator.pretty_printer} was [true]. *)

	val pp_B_position_elt_E :
		(Format.formatter -> Token.t -> unit) ->
		(Format.formatter -> 'a -> unit) ->
		Format.formatter -> ('a * 'a) * Token.t option -> unit
	(** Pretty printer for tuple that is parts of abstract syntax tree.
	Names of these functions depend on syntax.
	These functions be generated only if {!Castling.ParserGenerator.pretty_printer} was [true]. *)

	val pp_variant :
		(Format.formatter -> Token.t -> unit) ->
		(Format.formatter -> 'a -> unit) ->
		Format.formatter -> 'a variant -> unit
	(** Pretty printer for variant that is parts of abstract syntax tree.
	Names of these functions tree types depend on argument of calling {!Castling.ParserGenerator.one_of}.
	These functions be generated only if {!Castling.ParserGenerator.pretty_printer} was [true]. *)

	val pp_result :
		(Format.formatter -> Token.t -> unit) ->
		(Format.formatter -> 'a -> unit) ->
		Format.formatter -> 'a variant -> unit
	(** Pretty printer for abstract syntax tree that is result of parsing.
	This function be generated only if {!Castling.ParserGenerator.pretty_printer} was [true]. *)
end

(** This module has parsing functions.
This name [Parser] depends on [?parser_module] of calling {!Castling.ParserGenerator.generate}.
Contents of this module be expanded if [?parser_module] was not given. *)
module Parser : sig
	val debug : bool ref
	(** When [true], printing current state to stderr and checking infinity loop.
	The initial setting for [debug] is [false].
	This value be generated only if {!Castling.ParserGenerator.runtime_debug} was [true]. *)
	
	(** Variant holding parts of abstract syntax tree types.
	This type is internally used only in this module. *)
	type 'a value =
		| Value_B_position_elt_E of (('a * 'a) * Token.t option)
		| Value_elt of Token.t option
		| Value_variant of 'a AST.variant option;;
	
	val token_1 : char
	(** Token constant of ["1"] for internal use to make error value. *)
	
	val token_x : char
	(** Token constant of ["x"] for internal use to make error value. *)
	
	type error =
	    Unexpected of Token.t (** token unexpected but found *)
	  | Unexpected_z (** error case marked with {!Castling.ParserGenerator.error} found *)
	  | Expected of Token.t list (** one of tokens expected but not found *)
	  | Expected_variant (** non-terminal symbol expected but skipped *)
	(** This type represents an error parser detected.
	Non-terminal symbols prefixed [Expected_optional_] could be epsilon and has not to report necessity.
	The type of value that [Unexpected] and [Expected] holds is depend on [Token] parameter of {!Castling.ParserGenerator}. *)
	
	val pp_list : (Format.formatter -> 'a -> unit) -> Format.formatter -> 'a list -> unit
	(** Pretty printer for ['a list].
	This function be generated only if {!Castling.ParserGenerator.error_printer} was [true]. *)
	
	val pp_error : (Format.formatter -> Token.t -> unit) -> Format.formatter -> error -> unit
	(** Pretty printer for {!error}.
	This function be generated only if {!Castling.ParserGenerator.error_printer} was [true]. *)
	
	type 'a stack_item = {
		position : 'a * 'a;
		value : 'a value;
		goto_f : int -> 'a * 'a -> Token.t -> 'a t -> 'a t}
	(** Parsing state.
	This type is internally used only in this module.
	Positions be pair only if {!Castling.ParserGenerator.ranged_position} was [true]. *)
	and 'a t = {
		state_f : 'a * 'a -> Token.t -> 'a t -> 'a t;
		stack : 'a stack_item list;
		errors : (('a * 'a) * error) list;
		accepted : bool;
		mutable path : (int * int) list}
	(** Parser type.
	Please take this type like abstract data type.
	Positions be pair only if {!Castling.ParserGenerator.ranged_position} was [true].
	[.path] field be generated only if {!Castling.ParserGenerator.runtime_debug} was [true]. *)

	val state_debug : 'a t -> int -> unit
	(** Printing state number to stderr and checking infinity loop.
	This function is internally used only in this module.
	This function be generated only if {!Castling.ParserGenerator.runtime_debug} was [true]. *)
	
	val state_0 : 'a * 'a -> Token.t -> 'a t -> 'a t
	(** Given token dispatcher of state 0.
	This function is internally used only in this module.
	Third parameter be pair only if {!Castling.ParserGenerator.ranged_position} was [true]. *)
	
	val goto_0 : int -> 'a * 'a -> Token.t -> 'a t -> 'a t
	(** Reduced symbol dispatcher of state 0.
	This function is internally used only in this module.
	Third parameter be pair only if {!Castling.ParserGenerator.ranged_position} was [true]. *)

	val state_1 : 'a * 'a -> Token.t ->  'a t -> 'a t
	(** Given token dispatcher of state 1. *)

	val state_2 : 'a * 'a -> Token.t ->  'a t -> 'a t
	(** Given token dispatcher of state 2. *)

	val state_3 : 'a * 'a -> Token.t ->  'a t -> 'a t
	(** Given token dispatcher of state 3. *)

	val state_4 : 'a * 'a -> Token.t ->  'a t -> 'a t
	(** Given token dispatcher of state 4. *)

	val goto_error : int -> 'a * 'a -> Token.t ->  'a t -> 'a t
	(** Given token dispatcher of state has not to reduce, does [assert false].
	This function is internally used only in this module. *)

	val create : unit -> 'a t
	(** New parser. *)
	
	val post : 'a * 'a -> Token.t -> 'a t -> 'a t
	(** To give the parser next token with position.
	Third parameter be pair only if {!Castling.ParserGenerator.ranged_position} was [true].
	Otherwise, This function be [val post : 'a t -> 'a -> Token.t -> unit]. *)
	
	val accepted : 'a t -> bool
	(** To check which is parsing finished. *)

	val result : 'a t -> 'a AST.variant option
	(** To get abstract syntax tree that is result of parsing.
	When [accepted] returns [false], this function will fail [assert]. *)

	val errors : 'a t -> (('a * 'a) * error) list
	(** Detected errors.
	Positions be pair only if {!Castling.ParserGenerator.ranged_position} was [true].
	Otherwise, This function be [val errors : 'a t -> ('a * error) list]. *)

end
